package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.dromara.dao.business.domain.Settings;
import org.dromara.dao.business.mapper.SettingsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SettingsService {

    @Autowired
    SettingsMapper settingsMapper;


    public List<Settings> list() {
        return settingsMapper.selectList(new LambdaQueryWrapper<Settings>());
    }


    @Transactional
    public void updateSettings(List<Settings> settingsList) {
        if (CollectionUtils.isNotEmpty(settingsList)) {
            for (Settings settings : settingsList) {
                settingsMapper.updateSetting(settings.getSettingKey(), settings.getSettingValue());
            }
        }
    }


    public Map<String, Settings> getSettings() {
        Map<String, Settings> settingsMap = new HashMap<>();
        List<Settings> settings = settingsMapper.selectList(null);
        if (CollectionUtils.isNotEmpty(settings)) {
            for (Settings setting : settings) {
                settingsMap.put(setting.getSettingKey(), setting);
            }
        }
        return settingsMap;
    }




    public Map<String,String> getSettingsMap() {
        Map<String, String> settingsMap = new HashMap<>();
        List<Settings> settings = settingsMapper.selectList(null);
        if (CollectionUtils.isNotEmpty(settings)) {
            for (Settings setting : settings) {
                settingsMap.put(setting.getSettingKey(), setting.getSettingValue());
            }
        }
        return settingsMap;
    }
}
